package com.huitone.gddw.service.impl.system;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.entity.UserRole;
import com.huitone.gddw.dao.system.IUserRoleDao;
import com.huitone.gddw.service.system.IUserRoleService;
import com.huitone.gddw.utils.StringUtils;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 用户角色管理 接口实现类
 * @author wuluodan
 *
 */
@Service
public class UserRoleService implements IUserRoleService {

	@Autowired
	private IUserRoleDao userRoleDao;
	
	@Override
	public List<UserRole> getByUserId(String userId) {
		return userRoleDao.getByUserId(userId);
	}

	@Override
	public void changeUserRoles(String userId, String roleIds) {
		List<String> list = StringUtils.isBlank(roleIds) ? new ArrayList<String>() : Arrays.asList(roleIds.split(","));
		List<String> listSave = new ArrayList<String>();
		List<String> listDel = new ArrayList<String>();
		List<String> listAll = this.getRoleIdsByUserId(userId);
		for (String s : list) {
			if (!listAll.contains(s)) {
				listSave.add(s);
			}
		}
		for (String s : listAll) {
			if (!list.contains(s)) {
				listDel.add(s);
			}
		}
		this.updateByUserId(userId, listSave, listDel);
	}
	
	/**
	 * 根据用户ID保存/删除角色权限
	 * @param userId 用户ID
	 * @param listSave 保存列表
	 * @param listDel 删除列表
	 */
	private void updateByUserId(String userId, List<String> listSave, List<String> listDel) {
		List<UserRole> list = new ArrayList<UserRole>();
		for (String s : listSave) {
			UserRole entity = new UserRole();
			entity.setUserId(userId);
			entity.setRoleId(s);
			list.add(entity);
		}
		this.saveBatch(list);
		this.deleteByUserId(userId, listDel);
	}
	
	/**
	 * 批量保存
	 * @param list
	 */
	private void saveBatch(List<UserRole> list) {
		for (UserRole userRole : list) {
			userRole.setRecId(StringUtils.uuid());
			userRole.setInsertTime(new Date());
			userRole.setInsertStaffId(SysLoginUtil.getLoginUserId());
			userRole.setLastModTime(userRole.getInsertTime());
			userRole.setLastModStaffId(userRole.getInsertStaffId());
		}
		if (!list.isEmpty() && list.size() > 0) {
			userRoleDao.insertBatch(list);
		}
	}
	
	/**
	 * 批量删除
	 * @param userId 用户ID
	 * @param listDel
	 */
	private void deleteByUserId(String userId, List<String> listDel) {
		if (!listDel.isEmpty() && listDel.size() > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
			map.put("list", listDel);
			userRoleDao.deleteByUserId(map);
		}
	}
	
	/**
	 * 获取用户所有角色ID
	 * @param userId 用户ID
	 * @return List
	 */
	private List<String> getRoleIdsByUserId(String userId) {
		List<String> listRole = new ArrayList<String>();
		List<UserRole> list = this.getByUserId(userId);
		for (UserRole userRole : list) {
			listRole.add(userRole.getRoleId());
		}
		return listRole;
	}

	@Override
	public List<UserRole> getByRoleId(String roleId) {
		return userRoleDao.getByRoleId(roleId);
	}

	@Override
	public void delAllByUserId(String userId) {
		userRoleDao.delAllByUserId(userId);
	}

	@Override
	public void delAllByRoleId(String roleId) {
		userRoleDao.delAllByRoleId(roleId);
	}

}
